package com.example.blogindex.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blogindex.web.Fengin.OtherFengin;
import com.example.blogindex.web.Fengin.OtherFenginImpl;
import com.example.blogindex.web.Fengin.UserFengin;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xpd.blog.bean.Article;
import com.xpd.blog.bean.Result;
import com.xpd.blog.bean.Typeinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.List;

@RestController
public class IndexController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    UserFengin userFengin;
    @Autowired
    OtherFengin otherFengin;
    @Autowired
    OtherFenginImpl otherFenginImpl;
    @RequestMapping("index")
    public  String other(){
        return "index";
    }
    @HystrixCommand(fallbackMethod = "hys")
    @RequestMapping("user")
    public String user(){
        return restTemplate.getForObject("http://127.0.0.1:7000/user/user",String.class);
    }
    String hys(){
        return "restTemplate服务降级";
    }
    //得到所有的博客
    @RequestMapping("getAllArticle")
    public Result getAllArticle(@RequestBody Article article,
                              @RequestParam int page, @RequestParam int size){
        Result allArticle = otherFengin.getAllArticle(article,
                page, size);
        otherFenginImpl.setArticles(allArticle);
        return allArticle;
    }
    //得到类别
    @RequestMapping("getAllType")
    public List<Typeinfo> getAllType(){
        List<Typeinfo> allType = otherFengin.getAllType();
        otherFenginImpl.setTypeinfoList(allType);
        return allType;
    }

    //得到访问量最多的五个blog
    @RequestMapping("getNewBlog")
    public List<Article> getNewBlog(){
        List<Article> newBlog = otherFengin.getNewBlog();
        return newBlog;
    }

    //点击类别查找blog
    @RequestMapping("getArtByType")
    Result getArtByType(@RequestBody Article article, @RequestParam int page, @RequestParam int size,@RequestParam int tid){
        Result artByType = otherFengin.getArtByType(article,page, size, tid);
        return artByType;
    }
    //根据id来查找blog
    @RequestMapping("getArtByid")
    public Result  getArtByid(@RequestParam int aid){
        Result artByid = otherFengin.getArtByid(aid);
        System.out.println("artByid = " + artByid);
        return new Result(1,"查询成功",artByid);
    }

    //添加blog
    @RequestMapping("article/addArticle")
    public Result addArticle(@RequestBody Article article){
        Result result = otherFengin.addArticle(article);
        return result;
    }
    @RequestMapping("draftArticle")
    public Result draftArticle(@RequestBody Article article){
        Result result = otherFengin.draftArticle(article);
        return result;
    }
    //根据id来查找
    @RequestMapping("getAdmire")
    public Result getAdmire(@RequestParam int usid){
        Result admire = otherFengin.getAdmire(usid);
        return admire;
    }
    //得到自己的blog
    @RequestMapping("getMyBlog")
    public Result getMyBlog(@RequestParam int usid,@RequestParam int page,@RequestParam int size,@RequestParam int status){
        Result myBlog = otherFengin.getMyBlog(usid,page,size,status);
        return myBlog;
    }
    //删除自己的blog
    @RequestMapping("article/delMyBlog")
    public Result delMyBlog(@RequestParam int aid){
        Result result = otherFengin.delMyBlog(aid);
        return  result;
    }
    //修改自己的blog
    @RequestMapping("article/updateArticle")
    public Result updateArticle( @RequestBody Article article){
        Result result = otherFengin.updateArticle(article);
        return result;
    }
    //通过类别查找
    @RequestMapping("getBlogByType")
    public Result getBlogByType(@RequestParam int aid,@RequestParam int tid){
        Result blogByType = otherFengin.getBlogByType(aid, tid);
        return blogByType;
    }

    //得到所用的用户
    @RequestMapping("getAdminAllArticle")
    public Result getAllArticle(@RequestBody Article article,@RequestParam int page,@RequestParam int size,@RequestParam int tid,@RequestParam String endDate,@RequestParam String startDate,@RequestParam String searchUname){
        Result allArticle = otherFengin.getAdminAllArticle(article, page, size, tid,endDate,startDate,searchUname);
        return allArticle;
    }

    //修改blog状态
    @RequestMapping("statusByusid")
    public Result statusByusid(@RequestBody Article article,@RequestParam int status){
        Result result = otherFengin.statusByusid(article,status);
        return result;
    }




}
